package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
	// write your code here
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入第一个二进制字符串");
        String str1 = sc.next();
        System.out.println("请输入第二个二进制字符串");
        String str2 = sc.next();
        System.out.println("二进制" + str1 +"与二进制" + str2 + "的和为" + addBinary(str1,str2));
    }


    public static String addBinary(String a, String b) {
        StringBuffer buf = new StringBuffer();
        int index1 = a.length() - 1, index2 = b.length() - 1;
        int t = 0;
        while (index1 >= 0 || index2 >= 0 || t != 0) {
            int digit1 = index1 >= 0 ? a.charAt(index1) - '0' : 0;
            int digit2 = index2 >= 0 ? b.charAt(index2) - '0' : 0;
            int sum = digit1 + digit2 + t;
            buf.append(sum % 2);
            t = sum / 2;
            index1--;
            index2--;
        }
        return buf.reverse().toString();
    }
}
